package org.jeecg.modules.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import me.zhyd.oauth.utils.UuidUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.api.vo.album.AlbumDetail;
import org.jeecg.modules.api.vo.album.AlbumVo;
import org.jeecg.modules.api.vo.songlist.SonglistDetailVo;
import org.jeecg.modules.api.vo.songlist.SonglistRelatedVo;
import org.jeecg.modules.api.vo.songlist.TagVo;
import org.jeecg.modules.music.entity.*;
import org.jeecg.modules.music.entity.dto.HomeMusCountDto;
import org.jeecg.modules.music.entity.vo.HomeAlbumsVo;
import org.jeecg.modules.music.entity.vo.HomeBannerVo;
import org.jeecg.modules.music.entity.vo.HomeHotAlbumsVo;
import org.jeecg.modules.music.mapper.*;
import org.jeecg.modules.music.model.MusSongListCategoryModel;
import org.jeecg.modules.music.service.IMusAlbumSongListService;
import org.jeecg.modules.system.model.SysUserSysDepartModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MusAlbumSongListServiceImpl extends ServiceImpl<MusAlbumSongListMapper, MusAlbumSong> implements IMusAlbumSongListService {

    @Autowired
    private MusSongListCategoryMapper songListCategoryMapper;

    @Autowired
    private MusListSongMapper musListSongMapper;

    @Autowired
    private MusAlbumArtistMapper musAlbumArtistMapper;

    @Autowired
    private MusCollectSongMapper collectSongMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAlbumSong(MusAlbumSong albumSong) {
        this.save(albumSong);
        if(oConvertUtils.isNotEmpty(albumSong.getCategoryIds())){
            String[] arr = albumSong.getCategoryIds().split(",");
            for(String categoryId : arr){
                MusSongListCategory songListCategory = new MusSongListCategory(albumSong.getId(),categoryId);
                songListCategoryMapper.insert(songListCategory);
            }
        }
        if (oConvertUtils.isNotEmpty(albumSong.getArtistId())){
            MusAlbumArtist albumArtist = new MusAlbumArtist();
            albumArtist.setId(UUID.randomUUID().toString().replace("-", ""));
            albumArtist.setArtistId(albumSong.getArtistId());
            albumArtist.setAlbumId(albumSong.getId());
            musAlbumArtistMapper.insert(albumArtist);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editAlbumSong(MusAlbumSong songList) {

        if(oConvertUtils.isNotEmpty(songList.getCategoryIds())){
            if(oConvertUtils.isNotEmpty(songList.getId())){
                songListCategoryMapper.deleteByAlbumSongListId(songList.getId());
                String[] arr = songList.getCategoryIds().split(",");
                for(String categoryId : arr){
                    MusSongListCategory songListCategory = new MusSongListCategory(songList.getId(),categoryId);
                    songListCategoryMapper.insert(songListCategory);
                }
            }
        }
        MusAlbumSong albums = this.getById(songList.getId());
        if (oConvertUtils.isNotEmpty(albums.getArtistId())){
            musAlbumArtistMapper.deleteAlbumArtist(albums.getId(),albums.getArtistId());
        }
        //step.1 修改歌手基础信息
        this.updateById(songList);
        if (oConvertUtils.isNotEmpty(songList.getArtistId())){
            MusAlbumArtist abs = new MusAlbumArtist();
            abs.setId(UUID.randomUUID().toString().replace("-", ""));
            abs.setArtistId(songList.getArtistId());
            abs.setAlbumId(songList.getId());
            musAlbumArtistMapper.insert(abs);
        }
    }

    @Override
    public IPage<MusSong> querySongDetailPage(Page<MusSong> page, String songlistId, MusSong songParams) {
        return this.baseMapper.querySongDetailPage(page,songlistId,songParams);
    }



    @Override
    public IPage<MusSong> queryNoSongDetailPage(Page<MusSong> page, String songlistId, MusSong songParams) {
        return this.baseMapper.queryNoSongDetailPage(page,songlistId,songParams);
    }

    @Override
    public boolean saveSongs(String songlistId, String songIds) {
        List<String> list = Arrays.stream(songIds.split(",")).map(String::valueOf).collect(Collectors.toList());
        for(String songId : list){
            MusListSong listSong = new MusListSong();
            listSong.setSonglistId(songlistId);
            listSong.setId(UUID.randomUUID().toString().replace("-", ""));
            listSong.setSongId(songId);
            this.musListSongMapper.insert(listSong);
        }
        return false;
    }



    @Override
    public IPage<MusSongListCategoryModel> queryAlbumSongByOrgCode(String musCode, MusAlbumSong albumParams, IPage page) {
        List<MusSongListCategoryModel> list = baseMapper.getAlbumSongByOrgCode(page,musCode,albumParams);
        Integer total = baseMapper.getAlbumSongByOrgCodeTotal(musCode, albumParams);
        IPage<MusSongListCategoryModel> result = new Page<>(page.getCurrent(), page.getSize(), total);
        result.setRecords(list);
        return result;
    }

    @Override
    public IPage<MusAlbumSong> querySongListPage(Page<MusAlbumSong> page, String musCode, MusAlbumSong albumParams) {
        return this.baseMapper.querySongListPage(page,musCode,albumParams);
    }

    @Override
    public boolean deleteAlbum(String id) {
        this.removeById(id);
        songListCategoryMapper.deleteByAlbumSongListId(id);
        return false;
    }

    @Override
    public boolean deleteBatchAlbums(String ids) {
        List<String> list = Arrays.stream(ids.split(",")).map(String::valueOf).collect(Collectors.toList());
        for(String albumId : list){
            songListCategoryMapper.deleteByAlbumSongListId(albumId);
        }
        //1.删除用户
        this.removeByIds(Arrays.asList(ids.split(",")));
        return false;
    }

    @Override
    public boolean deleteListSong(String songlistId, String songId) {
        musListSongMapper.deleteListSong(songlistId,songId);
        return false;
    }
    @Override
    public boolean deleteListSongBatch(String songlistId, String songIds) {
        List<String> list = Arrays.stream(songIds.split(",")).map(String::valueOf).collect(Collectors.toList());
        for(String songId : list){
            musListSongMapper.deleteListSong(songlistId,songId);
        }
        return false;
    }

    @Override
    public List<HomeBannerVo> queryHomeBanner(String type) {
        return baseMapper.queryBanner(Integer.parseInt(type));
    }

    @Override
    public List<HomeAlbumsVo> queryHomeHotAlbums(String type, String limit,String area) {
        return baseMapper.queryHomeHotAlbums(Integer.parseInt(type),Integer.parseInt(limit),area);
    }

    @Override
    public List<HomeHotAlbumsVo> queryHotAlbumsAll() {
        return baseMapper.queryHomeAlbumsAll();
    }

    @Override
    public AlbumVo queryAlbumVoDetail(String albumId) {
        return baseMapper.queryAlbumVoDetail(albumId);
    }

    //获取专辑内容 /album
    @Override
    public AlbumDetail queryAlbumDetail(String id) {
        return baseMapper.getAlbumAPIDetail(id);
    }

    @Override
    public List<AlbumDetail> querySingerAlbums(String albumId, String artistId, Integer limit, Integer offset) {
        return baseMapper.querySingerAlbums(albumId,artistId,limit,offset);
    }

    @Override
    public SonglistDetailVo reqPlayListDetail(String id) {

        return baseMapper.reqPlayListDetail(id);
    }

    @Override
    public List<TagVo> getSonglistTags(String categoryIds) {
        String[] ids = categoryIds.split(",");
        return baseMapper.queryAlbumTags(ids);
    }

    @Override
    public List<SonglistRelatedVo> querySonglistRelated(String id) {
        return baseMapper.querySonglistRelated(id);
    }

    @Override
    public List<SonglistDetailVo> queryCatPlayList(String cat, Integer limit, Integer offset) {
        List<SonglistDetailVo> playlists = baseMapper.queryCatPlayList(cat,limit,offset);
        playlists.stream().map(SonglistDetailVo ->{
            SonglistDetailVo.setTags(baseMapper.queryAlbumTags(SonglistDetailVo.getCategoryIds().split(",")));
            SonglistDetailVo.setTrackCount(musListSongMapper.countAlbumSize(SonglistDetailVo.getId()));
            return SonglistDetailVo;
        }).collect(Collectors.toList());
        return playlists;
    }

    @Override
    public List<AlbumDetail> queryAlbums(String area, Integer offset, Integer limit) {
        return baseMapper.queryAlbums(area,offset,limit);
    }

    @Override
    public List<AlbumDetail> searchSuggestAlbums(String keywords) {
        return baseMapper.searchSuggestAlbums(keywords);
    }

    @Override
    public List<AlbumDetail> searchAlbums(String keywords, Integer limit, Integer offset) {
        return baseMapper.searchAlbums(keywords,limit,offset);
    }

    @Override
    public List<SonglistDetailVo> searchPlaylists(String keywords, Integer limit, Integer offset) {
        List<SonglistDetailVo> playlists = baseMapper.searchPlaylists(keywords,limit,offset);
        playlists.stream().map(SonglistDetailVo ->{
            SonglistDetailVo.setTags(baseMapper.queryAlbumTags(SonglistDetailVo.getCategoryIds().split(",")));
            SonglistDetailVo.setTrackCount(musListSongMapper.countAlbumSize(SonglistDetailVo.getId()));
            return SonglistDetailVo;
        }).collect(Collectors.toList());
        return playlists;
    }

    @Override
    public List<SonglistDetailVo> getUserSonglist(String uid) {
        List<SonglistDetailVo> playlists = baseMapper.getUserSonglist(uid);
        playlists.stream().map(SonglistDetailVo ->{
            SonglistDetailVo.setTags(baseMapper.queryAlbumTags(SonglistDetailVo.getCategoryIds().split(",")));
            SonglistDetailVo.setTrackCount(musListSongMapper.countAlbumSize(SonglistDetailVo.getId()));
            return SonglistDetailVo;
        }).collect(Collectors.toList());
        return playlists;

    }

    @Override
    public List<AlbumDetail> queryAlbumsByUser(String uid) {
        return baseMapper.queryAlbumsByUser(uid);
    }

    @Override
    public List<SonglistDetailVo> getUserMake(String uid) {
        List<SonglistDetailVo> list =  baseMapper.getUserMake(uid);
        list.stream().map(SonglistDetailVo ->{
            QueryWrapper<MusCollectSong> wrapper = new QueryWrapper<>();
            wrapper.eq("user_id",uid);
            SonglistDetailVo.setTrackCount(collectSongMapper.selectCount(wrapper));
            return SonglistDetailVo;
        }).collect(Collectors.toList());
        return list;
    }

    @Override
    public SonglistDetailVo queryPlayListUser(String id) {
        QueryWrapper<MusCollectSong> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        SonglistDetailVo playlist = baseMapper.queryPlayListUser(id);
        playlist.setTrackCount(collectSongMapper.selectCount(wrapper));
        return playlist;
    }

    @Override
    public List<HomeMusCountDto> queryGroupRecommend() {
        return baseMapper.queryGroupRecommend();
    }

    @Override
    public List<HomeMusCountDto> queryGroupArea() {
        return baseMapper.queryGroupArea();
    }

    @Override
    public List<HomeMusCountDto> queryGroupRecommendAlbums() {
        return baseMapper.queryGroupRecommendAlbums();
    }

    @Override
    public List<HomeMusCountDto> queryGroupAreaAlbums() {
        return baseMapper.queryGroupAreaAlbums();
    }
}
